﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace HaiFang
{
    /// <summary>
    /// Summary description for GeneralTypeInfoHandler
    /// </summary>
    public class GeneralTypeInfoHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["action"]))
            {
                string action = context.Request.QueryString["action"];
                switch (action)
                {
                    case "GetGeneralTypeInfo":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetGeneralTypeInfo(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作.");
                        break;
                }
            }
            else if (context.Request.Form.Count > 0)
            {
                string action = context.Request.Form["action"];
                switch (action)
                {
                    case "AddGeneralTypeInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseAddGeneralTypeInfo(context, Params);
                            }
                        }
                        break;
                    case "DelGeneralTypeInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseDelGeneralTypeInfo(context, Params);
                            }
                        }
                        break;
                    case "SaveGeneralTypeInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseSaveGeneralTypeInfo(context, Params);
                            }
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作");
                        break;
                }
            }
        }

        private void ResponseGetGeneralTypeInfo(HttpContext context, string Params)
        {
            try
            {
                IList<GeneralTypeInfo> list = ProxyGeneralType.QueryGeneralType(WebHelper.GetCurrentUserID(), Params);
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseAddGeneralTypeInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                GeneralTypeInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r=JsonHelper.JsonDeserialize<GeneralTypeInfo>(Params);
                    if (r != null)
                    {
                        ProxyGeneralType.InsertGeneralType(WebHelper.GetCurrentUserID(),1,0,r.GeneralType,r.GeneralValue1,out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseDelGeneralTypeInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                GeneralTypeInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<GeneralTypeInfo>(Params);
                    if (r != null)
                    {
                        ProxyGeneralType.InsertGeneralType(WebHelper.GetCurrentUserID(), 3, r.AutoID, r.GeneralType, r.GeneralValue1, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseSaveGeneralTypeInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                GeneralTypeInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<GeneralTypeInfo>(Params);
                    if (r != null)
                    {
                        ProxyGeneralType.InsertGeneralType(WebHelper.GetCurrentUserID(), 2, r.AutoID, r.GeneralType, r.GeneralValue1, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}